package com.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.mapper.BankMapper;
import com.mapper.UserWithdrawalMapper;
import com.model.*;
import com.util.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.message.UserMessage;

@Controller
@PropertySource("classpath:message.properties")
public class WithdrawalController {

	@Autowired
	private UserWithdrawalMapper userWithdrawalMapper;

	@Autowired
	private BankMapper bankMapper;

	@Autowired
	private UserMessage userMessage;
	
	@Autowired
	private Environment env;
	

	// inject via application.properties

	@Value("${welcome.message:test}")
	private String message = "Hello World";


	@RequestMapping("/withdrawal")
	public String withdrawal(Map<String, Object> model, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
		UserBank bank = new UserBank();
		bank.setUserId(user.getUserId());
		List<UserBank> bankList = bankMapper.getBanksByCondition(bank);
		model.put("list",bankList);
		model.put("pagename","withdrawal");
		return "usercenter";
	}


	@RequestMapping("/withdrawalSubmit")
	@ResponseBody
	public ResponseMsg withdrawalSubmit(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
		ResponseMsg msg = new ResponseMsg();

		String withdrawalAmount = request.getParameter("withdrawalAmount");
		String bankId = request.getParameter("bankId");

		UserBank tempbank = new UserBank();
		tempbank.setBankId(Integer.parseInt(bankId));
		List<UserBank> list = bankMapper.getBanksByCondition(tempbank);
		if(list.size() ==0){
			return msg;
		}

		UserBank bank = list.get(0);
		Withdrawal userWithdrawal = new Withdrawal();

		userWithdrawal.setBankname(bank.getBankName());
		userWithdrawal.setWithdrawalno(ConstantUtil.createWithdrawalNo());
		userWithdrawal.setBankno(bank.getBankNo());
		userWithdrawal.setUserid(user.getUserId());
		userWithdrawal.setRequestwithdrawalamount(new BigDecimal(withdrawalAmount));
		userWithdrawal.setProvince(bank.getProvince());
		userWithdrawal.setCity(bank.getCity());
		userWithdrawal.setBranchbankname(bank.getBranchBankName());
		userWithdrawal.setDistrict(bank.getDistrict());
		userWithdrawal.setStatus(true);

		userWithdrawalMapper.addUserWithdrawal(userWithdrawal);
		msg.setStatus( ResponseMsg.success );
		return msg;
	}
	
	
	



	public UserWithdrawalMapper getUserWithdrawalMapper() {
		return userWithdrawalMapper;
	}

	public BankMapper getBankMapper() {
		return bankMapper;
	}

	public void setBankMapper(BankMapper bankMapper) {
		this.bankMapper = bankMapper;
	}

	public void setUserWithdrawalMapper(UserWithdrawalMapper userWithdrawalMapper) {
		this.userWithdrawalMapper = userWithdrawalMapper;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public UserMessage getUserMessage() {
		return userMessage;
	}

	public void setUserMessage(UserMessage userMessage) {
		this.userMessage = userMessage;
	}

	public Environment getEnv() {
		return env;
	}

	public void setEnv(Environment env) {
		this.env = env;
	}

}
